var qs = require('qs');
export function login(usertype, that) {
  let type = null
  switch (usertype) {
    case '1':
      type = 'student'
      break;
    case '2':
      type = 'auditor'
      break;
    case '3':
      type = 'judges'
      break;
    case '4':
      type = 'admin'
      break;

    default:
      break;
  }
  let data = qs.stringify({
    name: that.form.name,
    password: that.form.password,
    type: type
  })
  return that.$http.post('/student/login', data, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
    .then((res) => {
      if (res.data.result === '成功') {
        let datas  = {
          name: res.data.userName
        }
        sessionStorage.setItem('user', JSON.stringify(datas))
        switch (usertype) {
          case '1':
            sessionStorage.setItem('usertype', '1')
            that.$router.push({
              path: '/index/home',
              query: {
                usertype: '1'
              }
            })
            break;
          case '2':
            sessionStorage.setItem('usertype', '2')
            that.$router.push({
              path: '/index/home',
              query: {
                usertype: '2'
              }
            })
            break;
          case '3':
            sessionStorage.setItem('usertype', '3')
            that.$router.push({
              path: '/index/home',
              query: {
                usertype: '3'
              }
            })
            break;
          case '4':
            sessionStorage.setItem('usertype', '4')
            that.$router.push({
              path: '/index/home',
              query: {
                usertype: '4'
              }
            })
            break;

          default:
            break;
        }
      } else {
        that.$message({
          message: res.data.result,
          type: 'error'
        });
      }
    })
  
}